const { UserOperations, User } = require('../../database/index')
const { getExecutedSql } = require('../../logger');
// 导出
module.exports = async (req, res) =>
{
    const operation = req.params.operation;
    const { userId, operationType, message, pageNum = 1, // 默认页码为1
        pageSize = 5, messageStatus = '', pagination = true, messagesContent, messageID } = req.body
    try {
        switch (operation) {
            case 'create':
                const data = {
                    user_id: userId,
                    operation_type: operationType,
                    operation_date: new Date(),
                    message: message,
                    content: messagesContent,
                    status: 1, //1为未读
                }
                const result = await UserOperations.create(data)
                res.status(200).send({
                    meta: {
                        msg: "创建成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'read':
                // debugger
                const whereCondition = {}
                if (messageStatus) {
                    whereCondition.status = messageStatus
                }
                if (operationType) {
                    whereCondition.operation_type = operationType
                }
                const total = await UserOperations.count({
                    where: whereCondition
                });  // 根据查询条件获取数据总数
                const offset = (pageNum - 1) * pageSize; // 计算偏移量
                if (pagination) {
                    const readResult = await UserOperations.findAll(
                        // 数据分页
                        {
                            include: [{
                                model: User, // 指定要包含的关联模型
                                attributes: ['UserName'], // 可选：指定要包含的部门属性
                            }],
                            where: whereCondition,
                            limit: Number(pageSize),
                            offset: Number(offset),
                            order: [
                                // 根据 operation_date 字段进行降序排序  
                                ['operation_date', 'DESC']
                                // ['operation_date', 'ASC']
                            ]
                        }
                    )
                    res.status(200).send({
                        data: {
                            readResult,
                            total,
                            pageSize: Number(pageSize),
                            pageNum: Number(pageNum)
                        },
                        meta: {
                            msg: "获取成功",
                            status: 200
                        },
                        sql: getExecutedSql()
                    })
                } else {
                    const readResult = await UserOperations.findAll(
                        // 数据分页
                        {
                            include: [{
                                model: User, // 指定要包含的关联模型
                                attributes: ['UserName'], // 可选：指定要包含的部门属性
                            }],
                            where: whereCondition,
                            order: [
                                // 根据 operation_date 字段进行降序排序  
                                ['operation_date', 'DESC']
                                // ['operation_date', 'ASC']
                            ]
                        }
                    )
                    res.status(200).send({
                        data: {
                            readResult,
                        },
                        meta: {
                            msg: "获取成功",
                            status: 200
                        },
                        sql: getExecutedSql()
                    })
                }
                break;
            case 'delete':
                const delData = await UserOperations.destroy({ where: { id: messageID } })
                res.status(200).send({
                    meta: {
                        msg: "删除成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'update':
                const stateData = { status: 2 }
                const upDepData = await UserOperations.update(stateData, { where: { id: messageID } },)
                res.status(200).send({
                    meta: {
                        msg: "编辑成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'getMessageByID':
                const messageContent = await UserOperations.findOne({
                    include: [{
                        model: User, // 指定要包含的关联模型
                        attributes: ['UserName'], // 可选：指定要包含的部门属性
                    }],
                    where: { id: messageID }
                })
                res.status(200).send({
                    data: messageContent,
                    meta: {
                        msg: "获取成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
        }
    } catch (error) {
        res.status(500).json({ message: 'Internal server error', error: error.message });
    }
}